<?php

namespace app\api\service;

use app\api\model\ArticleM;
use Exception;
use think\Db;

class ZiS
{
    public static function info($xing, $ming): array
    {
        $params         = [
            'xmbhx' => $xing,
            'xmbhm' => $ming,
        ];
        $r              = [];
        $zbh            = 0;
        $bihua          = '';
        $pinying        = '';
        $wuxing         = '';
        $jixiong        = '';
        $xmbhx          = preg_split('/(?<!^)(?!$)/u', trim($params['xmbhx']));//姓
        $xmbhm          = preg_split('/(?<!^)(?!$)/u', trim($params['xmbhm']));//名
        $params['name'] = implode($xmbhx) . implode($xmbhm);
        foreach ($xmbhx as $key => $value) {
            $c = Db::table('eb_jf')->where('jt', $value)->find();//简体对应繁体字
            if ($c) {
                $a = Db::table('eb_xmbh')->where('zi', $c['ft'])->find();//简体
            } else {
                $a = Db::table('eb_xmbh')->where('zi', $value)->find();//简体
            }
            $r[$key]['zi']     = $value;
            $r[$key]['bhs']    = (int)$a['kxzdbh'];
            $r[$key]['jieshi'] = $a['kxzdjs'];
            $r[$key]['swjzxj'] = $a['swjzxj'];
            $r[$key]['hzwx']   = $a['hzwx'];
            $r[$key]['jxyy']   = $a['jxyy'];
            $r[$key]['pinyin'] = 9;
            $r[$key]['hzwx']   = $a['hzwx'];
            $r[$key]['name']   = $params['name'];
            $r[$key]['tg']     = 0;
            $r[$key]['rg']     = 0;
            $r[$key]['dg']     = 0;
            $r[$key]['wg']     = 0;
            $r[$key]['zg']     = 0;
            $zbh               = $zbh + $r[$key]['bhs'];
            $bihua             = $bihua . ' ' . $r[$key]['bhs'] . ' ';
            $pinying           = $pinying . ' ' . $r[$key]['pinyin'] . ' ';
            $wuxing            = $wuxing . ' ' . $r[$key]['hzwx'] . ' ';
            $jixiong           = $jixiong . ' ' . $r[$key]['jxyy'] . ' ';
        }
        $x = count($xmbhx);
        foreach ($xmbhm as $key => $value) {
            $c = Db::table('eb_jf')->where('jt', $value)->find();//简体对应繁体字
            if ($c) {
                $a = Db::table('eb_xmbh')->where('zi', $c['ft'])->find();//繁体
            } else {
                $a = Db::table('eb_xmbh')->where('zi', $value)->find();//简体
            }
            $r[$key + $x]['zi']     = $value;
            $r[$key + $x]['bhs']    = (int)$a['mzbh'];
            $r[$key + $x]['jieshi'] = $a['kxzdjs'];
            $r[$key + $x]['swjzxj'] = $a['swjzxj'];
            $r[$key + $x]['hzwx']   = $a['hzwx'];
            $r[$key + $x]['jxyy']   = $a['jxyy'];
            $r[$key + $x]['pinyin'] = 9;
            $r[$key + $x]['hzwx']   = $a['hzwx'];
            $r[$key + $x]['name']   = $params['name'];
            $r[$key + $x]['tg']     = 0;
            $r[$key + $x]['rg']     = 0;
            $r[$key + $x]['dg']     = 0;
            $r[$key + $x]['wg']     = 0;
            $r[$key + $x]['zg']     = 0;
            $zbh                    = $zbh + $r[$key + $x]['bhs'];
            $bihua                  = $bihua . ' ' . $r[$key + $x]['bhs'] . ' ';
            $pinying                = $pinying . ' ' . $r[$key + $x]['pinyin'] . ' ';
            $wuxing                 = $wuxing . ' ' . $r[$key + $x]['hzwx'] . ' ';
            $jixiong                = $jixiong . ' ' . $r[$key + $x]['jxyy'] . ' ';
        }
        $r[0]['bihua']   = $bihua;
        $r[0]['pinyin']  = $pinying;
        $r[0]['wuxing']  = $wuxing;
        $r[0]['jixiong'] = $jixiong;
        if (count($xmbhx) == 1) //单姓
        {
            $r[0]['tg'] = $r[0]['bhs'] + 1;
            $r[0]['rg'] = $r[0]['bhs'] + $r[1]['bhs'];//姓的最下字和名的最上字
            if (count($xmbhm) == 1)//单名
            {
                $r[0]['dg'] = 1 + $r[1]['bhs'];//单名加1
                $r[0]['wg'] = 2;
            } else {
                $r[0]['dg'] = $r[1]['bhs'] + $r[2]['bhs']; //多名
                $r[0]['wg'] = $zbh - $r[0]['rg'] + 1;
            }
            $r[0]['zg'] = $zbh;
        }
        if (count($xmbhx) == 2) //复姓
        {
            $r[0]['tg'] = $r[0]['bhs'] + $r[1]['bhs'];
            $r[0]['rg'] = $r[1]['bhs'] + $r[2]['bhs'];//姓的最下字和名的最上字
            if (count($xmbhm) == 1)//单名
            {
                $r[0]['dg'] = 1 + $r[2]['bhs'];//单名加1
                $r[0]['wg'] = $zbh - $r[0]['rg'] + 1;
            } else {
                $r[0]['dg'] = $r[2]['bhs'] + $r[3]['bhs']; //多名
                $r[0]['wg'] = $zbh - $r[0]['rg'];
            }
            $r[0]['zg'] = $zbh;
        }
        return $r;
    }

    public static function format($str)
    {
        return str_replace('。', '。<br>', $str);
    }

    public static function split($str)
    {
        return array_reduce(mb_str_split($str), function ($carry, $item) {
            if (!empty(trim($item))) {
                $carry[] = $item;
            }
            return $carry;
        }, []);
    }

    public static function translate($xing, $ming, $content_show = false): array
    {
        $flag = count($xing) . count($ming);
        switch ($flag) {
            case '11';
                /**
                 * 单姓单名
                 * 天 = 姓 + 1
                 * 人 = 姓 + 名
                 * 地 = 名 + 1
                 * 变 = 姓 + 名
                 * 外 = 1 + 1
                 * 总 = 姓 + 名
                 */
                $x = self::mzbh($xing[0]);
                $m = self::mzbh($ming[0]);
                return [
                    'xingming' => [
                        ['zi' => $xing[0], 'bihua' => $x],
                        ['zi' => $ming[0], 'bihua' => $m],
                    ],
                    'tiange'   => self::article($content_show, $x + 1),
                    'renge'    => self::article($content_show, $x + $m),
                    'dige'     => self::article($content_show, $m + 1),
                    'biange'   => self::article($content_show, $x + $m),
                    'waige'    => self::article($content_show, 1 + 1),
                    'zongge'   => self::article($content_show, $x + $m),
                ];
            /**
             * 单姓双名
             * 天 = 姓 + 1
             * 人 = 姓 + 名1
             * 地 = 名1 + 名2
             * 变 = 姓 + 名2
             * 外 = 名2 + 1
             * 总 = 姓 + 名1 + 名2
             */
            case '12';
                $x  = self::mzbh($xing[0]);
                $m1 = self::mzbh($ming[0]);
                $m2 = self::mzbh($ming[1]);
                return [
                    'xingming' => [
                        ['zi' => $xing[0], 'bihua' => $x],
                        ['zi' => $ming[0], 'bihua' => $m1],
                        ['zi' => $ming[1], 'bihua' => $m2],
                    ],
                    'tiange'   => self::article($content_show, $x + 1),
                    'renge'    => self::article($content_show, $x + $m1),
                    'dige'     => self::article($content_show, $m1 + $m2),
                    'biange'   => self::article($content_show, $x + $m2),
                    'waige'    => self::article($content_show, $m2 + 1),
                    'zongge'   => self::article($content_show, $x + $m1 + $m2),
                ];
            /**
             * 复姓单名
             * 天 = 姓1 + 姓2
             * 人 = 姓2 + 名
             * 地 = 名 + 1
             * 变 = 姓2 + 名
             * 外 = 姓1 + 1
             * 总 = 姓1 + 姓2 + 名
             */
            case '21';
                $x1 = self::mzbh($xing[0]);
                $x2 = self::mzbh($xing[1]);
                $m  = self::mzbh($ming[0]);
                return [
                    'xingming' => [
                        ['zi' => $xing[0], 'bihua' => $x1],
                        ['zi' => $xing[1], 'bihua' => $x2],
                        ['zi' => $ming[0], 'bihua' => $m],
                    ],
                    'tiange'   => self::article($content_show, $x1 + $x2),
                    'renge'    => self::article($content_show, $x2 + $m),
                    'dige'     => self::article($content_show, $m + 1),
                    'biange'   => self::article($content_show, $x2 + $m),
                    'waige'    => self::article($content_show, $x1 + 1),
                    'zongge'   => self::article($content_show, $x1 + $x2 + $m),
                ];
            /**
             * 复姓双名
             * 天 = 姓1 + 姓2
             * 人 = 姓2 + 名1
             * 地 = 名1 + 名2
             * 变 = 姓2 + 名2
             * 外 = 姓1 + 名2
             * 总 = 姓1 + 姓2 + 名1 + 名2
             */
            case '22';
                $x1 = self::mzbh($xing[0]);
                $x2 = self::mzbh($xing[1]);
                $m1 = self::mzbh($ming[0]);
                $m2 = self::mzbh($ming[1]);
                return [
                    'xingming' => [
                        ['zi' => $xing[0], 'bihua' => $x1],
                        ['zi' => $xing[1], 'bihua' => $x2],
                        ['zi' => $ming[0], 'bihua' => $m1],
                        ['zi' => $ming[1], 'bihua' => $m2],
                    ],
                    'tiange'   => self::article($content_show, $x1 + $x2),
                    'renge'    => self::article($content_show, $x2 + $m1),
                    'dige'     => self::article($content_show, $m1 + $m2),
                    'biange'   => self::article($content_show, $x2 + $m2),
                    'waige'    => self::article($content_show, $x1 + $m2),
                    'zongge'   => self::article($content_show, $x1 + $x2 + $m1 + $m2),
                ];
            default:
                throw new Exception('error switch');
        }
    }

    private static function mzbh($str)
    {
        $ft = Db::table('eb_jf')->where('jt', $str)->value('ft');
        if ($ft) {
            $str = $ft;
        }
        $mzbh = Db::table('eb_xmbh')->where('zi', $str)->value('mzbh');
        if (!$mzbh) {
            throw new Exception('error mzbh');
        }
        return $mzbh;
    }

    private static function article($content_show, $bihua): array
    {
        $content = ArticleM::where('title', $bihua)->value('content');
        if (!$content) {
            throw new Exception('error article empty ' . $bihua);
        }
        if (!preg_match('|\((.*)\)|U', $content, $match)) {
            throw new Exception('error article jixiong ' . $bihua);
        }
        return [
            'bihua'   => $bihua,
            'content' => $content_show ? $content : '',
            'jixiong' => trim($match[1]),
            'wuxing'  => (function ($title) use ($bihua) {
                $flag = (substr($title, -1, 1));
                if ($flag == 1 || $flag == 2) {
                    return '木';
                } else if ($flag == 3 || $flag == 4) {
                    return '火';
                } else if ($flag == 5 || $flag == 6) {
                    return '土';
                } else if ($flag == 7 || $flag == 8) {
                    return '金';
                } else if ($flag == 9 || $flag == 0) {
                    return '水';
                } else {
                    throw new Exception('error article wuxing' . $bihua);
                }
            })($bihua),
        ];
    }
}
